var util = require("../../../utils/util.js"),
{api} = require("../../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order_no: "",
        detail: null,
        goods_type: 1,
        autosize: {
            minHeight: 100
        },
        orderGoodsList: [],
        cart_id: null,
        playType: '1',
        loading: false,
        message: '',
        totalMoney: 0,
        userInfo:null,
        storeDetail:null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let orderGoodsList = wx.getStorageSync('orderGoodsList');
        orderGoodsList = Array.isArray(orderGoodsList) ? orderGoodsList : []
        this.setData({
            goods_type: options.goods_type || 1,
            cart_id: options.cart_id || null,
            goods_id: options.goods_id,
            orderGoodsList
        })
        let money = 0;
        orderGoodsList.forEach(item => {
            if (this.data.goods_type == 1) {
                money += Number(item.price)
            } else {
                money += Number(item.integral)
            }
        })
        this.setData({
            totalMoney: money
        })
        this.getUsers();
    },
    async getUsers() {
        const data = await app.getUsers();
        data && this.setData(data)
    },
    onChange(e) {
        this.setData({
            playType: e.detail
        })
    },
    onChangeMsg(e) {
        this.setData({
            message: e.detail
        })
    },
    onSubmit() {
        let {userInfo,goods_type,playType,cart_id,goods_id,message,totalMoney,storeDetail}=this.data;
        console.log('storeDetail',storeDetail);
        if(!storeDetail || !storeDetail.id){
            return wx.showToast({icon: 'none',title: '请选择预约门店！'});
        }
        if(goods_type == 1){
            if(playType == 2 && userInfo.money<totalMoney){
                return wx.showToast({icon: 'none',title: '您的钱包金额不足！'});
            }
        }else{
            if(userInfo.score<totalMoney){
                return wx.showToast({icon: 'none',title: '您的积分不足！'});
            }
        }
        this.setData({
            loading: true
        });
        let data = {
            is_wallet_payment: (playType == 1 ? 0 : 1),
            remarks:message,
            store_id:storeDetail.id
        };
        if (cart_id) {
            data.cart_ids = cart_id;
            api.orderCart(data).then(res => {
                if (res.code == 1 && res.data.order_no) {
                    this.moneyWxPlay(res.data.order_no)
                }
            }).catch(err=>this.setData({loading:false}))
        } else {
            data.goods_id = goods_id;
            api.orderCreate(data).then(res => {
                if (res.code == 1 && res.data.order_no) {
                    if(goods_type == 1 && playType == 1){
                        this.moneyWxPlay(res.data.order_no)
                    }else{
                        this.pointCartPlay(res.msg?res.msg:'');
                    }
                }
            }).catch(err=>this.setData({loading:false}))
        }
    },
    // 金额支付
    moneyWxPlay(order_no){
        api.againPalyOrder({order_no},()=>{
            this.setData({loading: false})
        })
        .then(play=>{
            let payment=play.data && play.data.payment && play.data.payment.paySign?play.data.payment:null;
            if(!payment){
                this.setData({loading: false})
                return wx.showToast({icon: 'none',title: '支付参数错误'});
            }
            return app.moneyWxPlay(payment)
        })
        .then(result=>this.playResult('支付成功！'+(result.msg?result.msg:'')))
        .catch(err=>{
            console.log('err',err);
            this.playResult('支付失败！');
        })
    },
    // 积分或支付
    pointCartPlay(msg){
        this.playResult('兑换成功！'+msg);
    },
    playResult(msg) {
        this.setData({loading: false})
          app.showError(msg,()=>{
            wx.redirectTo({
                url: '/pages/mine/order/order?type=0',
            })
          })
    },
    changeStore(){
        wx.navigateTo({
          url: '/pages/shopping/store/store?id='+this.data.goods_id+'&type=3',
        })
    },
    setStoreDetail(detail){
        this.setData({storeDetail:detail})
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        return util.shareEvent(e)
      }
})